package com.example.vfu_telia;

import java.io.IOException;
import java.text.DecimalFormat;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.location.Location;

public class OpenCellID {
	
	private String get;
	private String key;
	private HttpResponse response;
	private String responseString;
	private String [] responseStringArray;
	private boolean exec = false;
	
	/*
	 * [0] = mcc
	 * [1] = mnc
	 * [2] = cellid
	 * [3] = lac
	 * [4] = longitude
	 * [5] = latitude 
	 */
	private String [] cellInfo = new String [6];
	
	public OpenCellID(String apiKey){
		this.key = apiKey;
	}
	
	public double getDistance(String mcc, String mnc, String cellid, String lac, double currentLocationLongitude, double currentLocationLatitude){
		if (cellInfo[0] != null && cellInfo[0].equals(mcc) && cellInfo[1] != null && cellInfo[1].equals(mnc) && cellInfo[2] != null && cellInfo[2].equals(cellid) && cellInfo[3] != null && cellInfo[3].equals(lac)){
			if (cellInfo[4] != null && cellInfo[5] != null){
				return distance(currentLocationLongitude, currentLocationLatitude, Double.parseDouble(cellInfo[4]), Double.parseDouble(cellInfo[5]));
			} else {
				return -2;
			}
		} else {
			setCellInfo(mcc, mnc, cellid, lac);
			return -3;
		}
	}
	
	public String getDistanceToCellTowerString(String mcc, String mnc, String cellid, String lac, double currentLocationLongitude, double currentLocationLatitude){
		if (currentLocationLongitude != 0 && currentLocationLatitude != 0){
			Double value = getDistance(mcc, mnc, cellid, lac, currentLocationLongitude, currentLocationLatitude);
			if (value >= 0){
				return value.toString();
			} else if (value == -2){
				return "No data...";
			} else if (value == -3){
				return "Waiting...";
			} else {
				return "err";
			}
		} else {
			return "Waiting...";
		}
	}

	private double distance(double currentLocationLongitude, double currentLocationLatitude, double cellLocationLongitude, double cellLocationLatitude) {
		// TODO Auto-generated method stub
		Location loc1 = new Location("");
		loc1.setLatitude(currentLocationLatitude);
		loc1.setLongitude(currentLocationLongitude);
		
		Location loc2 = new Location("");
		loc2.setLatitude(cellLocationLatitude);
		loc2.setLongitude(cellLocationLongitude);
		
		return Math.round(loc1.distanceTo(loc2));
	}

	private void setCellInfo(String mcc, String mnc, String cellid, String lac){
		if (exec == false){
			exec = true;
		
		cellInfo[0] = mcc;
		cellInfo[1] = mnc;
		cellInfo[2] = cellid;
		cellInfo[3] = lac;
		cellInfo[4] = null;
		cellInfo[5] = null;
		
		setGetString();
		new Thread(new Runnable() {
		    public void run() {
		      try {
		    	  	  execRequest();
		    	  
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				exec = false;
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				exec = false;
				e.printStackTrace();
			}
		    }
		  }).start();
		exec = false;
		}
	}
	
	private void setGetString(){
		
		get = "http://www.opencellid.org/cell/get?" +
				"mcc=" + cellInfo[0] +
				"&mnc=" + cellInfo[1] +
				"&cellid=" + cellInfo[2] +
				"&lac=" + cellInfo[3] +
				"&key=" + key;
		
	}
	
	public void execRequest() throws ClientProtocolException, IOException{

		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(this.get);
		response = client.execute(request);
		responseString = EntityUtils.toString(response.getEntity());
		
		responseStringArray = responseString.split(" |,|\"");
		
		if (responsOK()){
			cellInfo[5] = responseStringArray[11];
			cellInfo[4] = responseStringArray[14];
		} else {
			cellInfo[4] = null;
			cellInfo[5] = null;
		}
		
		
	}
	
	private boolean responsOK(){
		for (int i = 0; i < responseStringArray.length; i++){
			if (responseStringArray[i].equals("stat=") && responseStringArray[i+1].equals("ok")){
				return true;
			}
		}
		return false;
	}
}
